package com.tx.zlm_replay.common;

import java.io.Serial;

/**
 * @author tx
 * @version 1.0
 * @date 2024/3/18 11:22
 * @description: 带数据的返回对象
 */
public class RwithD<T> extends R{
    @Serial
    private static final long serialVersionUID = -2041294155272933148L;
    // 数据
    private T data;
    public RwithD(String code, String message,T data) {
        super(code, message);
        this.data = data;
    }

    public RwithD(String code, String message) {
        super(code, message);
        this.data = null;
    }

    public RwithD(){}

    public T getData() {
        return data;
    }

    public RwithD<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 成功消息和数据
     * @param data 数据
     * @return RwithD
     */
    public static <T> RwithD<T> ok(T data){
        return new RwithD<>(RCode.SUCCESS.getCode(),RCode.SUCCESS.getMsg(),data);
    }

    /**
     * 成功消息
     * @return RwithD
     */
    public static <T> RwithD<T> ok(){
        return new RwithD<>(RCode.SUCCESS.getCode(),RCode.SUCCESS.getMsg(),null);
    }

    /**
     * 返回状态和数据
     * @param code code and name
     * @param data 数据
     * @return RwithD
     */
    public static <T> RwithD<T> build(RCode code,T data){
        return new RwithD<>(code.getCode(),code.getMsg(),data);
    }

    public static RwithD fail(String msg){
        return new RwithD(RCode.FAIL.getCode(), msg);
    }
    public static RwithD fail(String code,String msg){
        return new RwithD(code, msg);
    }
    /**
     * 带数据的失败信息
     * @param data 数据
     * @return RwithD
     */
    public static <T> RwithD<T> fail(T data){
        return new RwithD<>(RCode.FAIL.getCode(),RCode.FAIL.getMsg(),data);
    }

    /**
     * 不带数据的失败信息
     * @return fail
     */
    public static <T> RwithD<T> fail(){
        return new RwithD<>(RCode.FAIL.getCode(),RCode.FAIL.getMsg(),null);
    }

    @Override
    public String toString() {
        return "{" +
                "data:" + data + "," +
                super.toString() +
                "}";
    }
}
